﻿Imports System.Text

Public Class frmStartCompleteAutoPlateCheck
    Private objAP As New clsAutoPlantingDB
    Private Const UpSet As Integer = -1 '上调
    Private Const DownSet As Integer = 1 '下调

    Private Class ST_ZG
        Public rack_ID As String '料盒号
        Public ID As String '报头
    End Class
    Private Class ST_WG

        Public ID As String '报头
        Public result As String '结果
        Public PL1A1 As Integer
        Public PL1B1 As Integer
        Public PL1C1 As Integer
        Public PL1D1 As Integer
        Public PL1E1 As Integer
        Public PL1F1 As Integer
        Public PL1G1 As Integer
        Public PL1H1 As Integer
        Public PL1I1 As Integer
        Public PL1J1 As Integer
        Public PL1K1 As Integer
        Public PL1L1 As Integer
        Public PL1M1 As Integer
        Public PL1N1 As Integer
        Public PL1O1 As Integer
        Public PL1P1 As Integer
        Public PL1Q1 As Integer
        Public PL1R1 As Integer
        Public PL1S1 As Integer
        Public PL1T1 As Integer
        Public PL1Z1 As Integer
        Public PL127 As Integer
        Public PL128 As Integer
        Public PL129 As Integer
        Public PL130 As Integer
    End Class
    Private Sub frmStartCompleteAutoPlateCheck_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dgv.DataSource = objAP.getWorkList

        m_cEquipCommandControl = New clsEquipCooperateCommand("127.0.0.1", "3400")

    End Sub

    Private Sub btnUP_Click(sender As Object, e As EventArgs) Handles btnUP.Click
        setUpDown(UpSet)
    End Sub

    ''' <summary>
    ''' UP、Down
    ''' </summary>
    ''' <param name="flg">-1：上调，1：下调</param>
    Private Sub setUpDown(flg As Integer)
        If dgv.SelectedRows.Count < 1 Then Exit Sub

        If MsgBox(getMsgStr("MSG10064", IIf(flg = UpSet, "上调", "下调")), MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
            Dim lot1 As String = dgv.SelectedRows(0).Cells("LOT_NO").Value
            Dim ut1 As String = dgv.SelectedRows(0).Cells("colUPDATE_TIME").Value
            Dim lot2 As String = ""
            Dim ut2 As String = ""
            Dim intSelectRowIndex As Integer = dgv.SelectedRows(0).Index

            If flg = UpSet Then
                If intSelectRowIndex <> 0 Then
                    lot2 = dgv.Rows(dgv.SelectedRows(0).Index + flg).Cells("LOT_NO").Value
                    ut2 = dgv.Rows(dgv.SelectedRows(0).Index + flg).Cells("colUPDATE_TIME").Value
                End If
            Else
                If intSelectRowIndex < dgv.Rows.Count - 1 Then
                    lot2 = dgv.Rows(dgv.SelectedRows(0).Index + flg).Cells("LOT_NO").Value
                    ut2 = dgv.Rows(dgv.SelectedRows(0).Index + flg).Cells("colUPDATE_TIME").Value
                End If
            End If

            If lot2 <> "" Then
                If objAP.changeUpdateTime(lot1, ut1, lot2, ut2) = 1 Then

                    dgv.DataSource = objAP.getWorkList
                    dgv.Rows(intSelectRowIndex).Selected = False
                    dgv.Rows(intSelectRowIndex + flg).Selected = True

                End If
            End If
        End If
    End Sub

    Private Sub btnDown_Click(sender As Object, e As EventArgs) Handles btnDown.Click
        setUpDown(DownSet)
    End Sub

#Region "装置連携コマンド通信　自動着完工"
    ''' <summary>
    ''' 結果報告コマンドの送信
    ''' </summary>
    ''' <param name="commandName">コマンド</param>
    ''' <param name="result">結果</param>
    Protected Function sendResponseCommand(ByVal commandName As String, ByVal result As String) As Boolean
        Dim sendCommand = createResponseCommandStr(commandName, result, g_CURRENT_EQUIP_ID)
        PutLog(Me.ToString, "sendCommand", sendCommand)
        Return m_cEquipCommandControl.CommandSend(sendCommand)

    End Function

    ' TCPクライアント用イベント
    Protected WithEvents m_cEquipCommandControl As clsEquipCooperateCommand
    Delegate Sub TCPCLEventDelegate(e As TCPListener.TCPEventArgs)
    ''' <summary>
    ''' イベント受信
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub TCPCLEvent(sender As System.Object, e As TCPListener.TCPEventArgs) Handles m_cEquipCommandControl.tcpevent
        Debug.WriteLine("TCPCLEvent in")
        Dim adrData As New TCPCLEventDelegate(AddressOf DoTCPCLEvent)
        Try
            Me.BeginInvoke(adrData, e)
        Catch ex As Exception
            MsgBox(getMsgStr("MSG00186"))
        End Try
    End Sub
    Private Sub DoTCPCLEvent(e As TCPListener.TCPEventArgs)
        HideCommandWaiting()
        ' 接続完了
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Connect Then
            Debug.WriteLine("接続完了")
            MsgBox("接続完了")
        End If
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Shutdown Then
            Debug.WriteLine("接続切断")
            MsgBox("接続切断")
        End If
        ' データ受信
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Receive Then
            'MsgBox("データ受信") 'TODO:DEL
            Debug.WriteLine("データ受信")
            Dim receiveDataStr = Encoding.ASCII.GetString(e.receiveData, 0, e.receiveData.Count())
            Debug.WriteLine(receiveDataStr)
            PutLog(Me.ToString, "RECEIVEDATA", receiveDataStr)

            ' 受信コマンド解析
            CommandReceive(receiveDataStr)

        End If
        ' Exception
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Exception Then
            Debug.WriteLine("Exception受信")
            Debug.WriteLine(e.exception.Message)
            CommandReceive(Nothing)
        End If
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Timeout Then
            Debug.WriteLine("timeout受信")
            CommandReceive(Nothing)
        End If
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Cancel Then
            Debug.WriteLine("Cancel受信")
            CommandReceive(Nothing)
        End If
        If e.eventType = TCPListener.TCPEventArgs.TcpEventType.Retry Then
            Debug.WriteLine("retry受信")
            m_cEquipCommandControl.CommandConnect()
        End If
    End Sub
#End Region

#Region "自動着完工　ほかの処理"
    ''' <summary>
    ''' コマンド受信
    ''' 受信コマンドの解析結果を各画面に渡す
    ''' </summary>
    ''' <param name="receiveData">受信コマンド</param>
    Private Sub CommandReceive(receiveData As String)
        'MsgBox("Baseクラスの「CommandReceive」メソッドに入った！")
        HideCommandWaiting()
        If IsNothing(receiveData) Then
            Exit Sub
        End If

    End Sub

    Private Function commandAnalyse(receiveData As String) As ST_WG

    End Function

    Public Sub PutLog(ByVal szSender As String, ByVal szOperator As String, ByVal szMsg As String)

    End Sub
    Protected Sub HideCommandWaiting()
        Me.Cursor = Cursors.Default

        Me.Enabled = True
    End Sub

    Private Sub lblEquipTxt_Click(sender As Object, e As EventArgs) Handles lblEquipTxt.Click

    End Sub
#End Region
End Class